package com.jiuyin.dianjing.ui.activity.base;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.jiuyin.dianjing.R;
import com.jiuyin.dianjing.adapter.BaseRecyclerAdapter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;

import java.util.List;

import butterknife.BindView;
import ezy.ui.layout.LoadingLayout;

import static com.jiuyin.dianjing.api.constant.ApiConstant.PAGE_FIRST;


public abstract class BaseRefreshActivity<T> extends BaseActivity {
    @BindView(R.id.rcv_page)
    public RecyclerView mRcvPage;
    @BindView(R.id.srl_record_layout)
    public SmartRefreshLayout mSrlRecordLayout;
    @BindView(R.id.loading)
    public LoadingLayout mLoadingLayout;

    protected int mPageNum = PAGE_FIRST;
    private int mPageSize;
    protected BaseRecyclerAdapter<T> mAdapter;

    public BaseRefreshActivity(int size) {
        mPageSize = size;
    }

    protected void initRefreshView() {
        mSrlRecordLayout.setEnableAutoLoadMore(true);
        mSrlRecordLayout.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                fetchData();
            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                mPageNum = PAGE_FIRST;
                fetchData();
            }
        });
        mLoadingLayout.setEmptyText(getString(R.string.empty_tip));
    }

    @Override
    protected void initOthers() {
        initRefreshView();
    }

    protected abstract void fetchData();

    /**
     * 得到大小
     *
     * @return
     */
    protected int getRecycleViewSize() {
        if (null != mAdapter) {
            return mAdapter.getItemCount();
        } else {
            return 0;
        }
    }


    /**
     * 显示空
     */
    protected void showEmpty() {
        if (0 == getRecycleViewSize()) {
            if (null != mLoadingLayout) {
                mLoadingLayout.showEmpty();
            }
            if (null != mSrlRecordLayout) {
                mSrlRecordLayout.setEnableLoadMore(false);
            }
        }
    }

    /**
     * 隐藏空白页
     */
    protected void showContent() {
        if (null != mSrlRecordLayout) {
            mSrlRecordLayout.finishRefresh();
        }
        if (0 == getRecycleViewSize()) {
            if (null != mLoadingLayout) {
                mLoadingLayout.showContent();
            }
            if (null != mSrlRecordLayout) {
                mSrlRecordLayout.setEnableLoadMore(true);
            }
        }
    }

    /**
     * 处理加载状态
     */
    protected void loadDataFinished() {
        if (null != mSrlRecordLayout) {
            mSrlRecordLayout.finishRefresh();
            if (0 == getRecycleViewSize()) {
                mSrlRecordLayout.finishLoadMore();
            } else {
                mSrlRecordLayout.finishLoadMoreWithNoMoreData();
            }
        }
    }

    /**
     * 刷新数据
     *
     * @param models
     */
    protected void onRefresh(List<T> models) {
        showContent();
        int size = models.size();
        if (PAGE_FIRST == mPageNum) {
            mAdapter.refresh(models);
        } else {
            mAdapter.loadMore(models);
        }
        if (null != mSrlRecordLayout) {
            if (size < mPageSize) {
                mSrlRecordLayout.finishLoadMoreWithNoMoreData();
            } else {
                mSrlRecordLayout.finishLoadMore();
            }
        }
        mPageNum++;
    }

    /**
     * 处理网络内容
     * 显示内容
     *
     * @param data
     */
    protected void dealWithSuccessInfo(List<T> data) {
        if (null != data) {
            int size = data.size();
            if (0 != size) {
                onRefresh(data);
            } else {
                dealOtherInfo();
            }
        } else {
            dealOtherInfo();
        }
    }

    /**
     * 处理其它不正确的信息
     */
    protected void dealOtherInfo() {
        loadDataFinished();
        showEmpty();
    }
}
